/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.note.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.spi.http.HttpContext;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.note.entity.Note;
import com.thinkgem.jeesite.modules.note.service.NoteService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 笔记增删改查Controller
 * @author liyutg
 * @version 2017-07-26
 * frontPath
 * adminPath
 */



@Controller
@RequestMapping(value = "i/note/note")
public class NoteController extends BaseController {

	//HttpContext.Response.AddHeader("Access-Control-Allow-Credentials", "true");
	//HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "请求本Action的域名");//实现跨域

	@Autowired
	private NoteService noteService;
	
	@ModelAttribute
	public Note get(@RequestParam(required=false) String id) {
		Note entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = noteService.get(id);
		}
		if (entity == null){
			entity = new Note();
		}
		return entity;
	}
	
	@RequiresPermissions("note:note:view")
	@RequestMapping(value = {"list", ""})
	public String list(Note note, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Note> page = noteService.findPage(new Page<Note>(request, response), note); 
		model.addAttribute("page", page);
		return "modules/note/noteList";
	}

	@RequiresPermissions("note:note:view")
	@RequestMapping(value = "form")
	public String form(Note note, Model model) {
		model.addAttribute("note", note);
		return "modules/note/noteForm";
	}

	@RequiresPermissions("note:note:edit")
	@RequestMapping(value = "save")
	public String save(Note note, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, note)){
			return form(note, model);
		}
		noteService.save(note);
		addMessage(redirectAttributes, "保存笔记成功");
		return "redirect:"+Global.getAdminPath()+"/note/note/?repage";
	}
	
	@RequiresPermissions("note:note:edit")
	@RequestMapping(value = "delete")
	public String delete(Note note, RedirectAttributes redirectAttributes) {
		noteService.delete(note);
		addMessage(redirectAttributes, "删除笔记成功");
		return "redirect:"+Global.getAdminPath()+"/note/note/?repage";
	}

	//添加笔记
	@RequestMapping(value = "add_note")
	@ResponseBody
	public Map<String,String> add_note(String userId, String title, String text) {
		String type="1";
		Map<String,String> map=new HashMap<String, String>();
		if(userId!=null){
			Date  time= new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String creatTime=formatter.format(time);
			//创建笔记默认类型 "1"
			noteService.insertNote("1",creatTime,creatTime,title,text,userId);
			map.put("result","1");
			map.put("message","添加成功");
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}

	//返回最新笔记
	@RequestMapping(value = "getLast")
	@ResponseBody
	public Map<String,String> getLast(String userId) {
		String noteId;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String,String> map=new HashMap<String, String>();
		if(userId!=null){
			noteId=noteService.getNoteId(userId);
			if(noteId!=null){
				Note note=noteService.getNote(noteId);

				map.put("text",note.getNoteText());
				map.put("title",note.getNoteTitle());
				map.put("type",note.getNoteType());
				map.put("updateTime",formatter.format(note.getNoteTimeCreate()));
				map.put("createTime",formatter.format(note.getNoteTimeUpdate()));
				map.put("noteId",note.getNoteId());
				map.put("userId",note.getUser());

				map.put("result","1");
				map.put("message","成功找到");

			}else{
				map.put("result","0");
				map.put("message","该用户笔记数目为0");
			}
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}

	//查找笔记
	@RequestMapping(value = "search")
	@ResponseBody
	public Map<String,String> search(String userId, String title) {
		Map<String,String> map=new HashMap<String, String>();
		if(userId!=null&&title!=null){
			String text=null;
			String noteId=null;
			title="%"+title+"%";
			noteId=noteService.search(title,userId);
			if(noteId!=null){
				Note note=noteService.getNote(noteId);

				Date createTime = note.getNoteTimeCreate();
				Date updateTime = note.getNoteTimeUpdate();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dateCreate = formatter.format(createTime);
				String updateCreate = formatter.format(updateTime);


				map.put("createTime",dateCreate);
				map.put("updateTime",updateCreate);
				map.put("title",note.getNoteTitle());
				map.put("text",note.getNoteText());
				map.put("noteId",noteId);
				map.put("type",note.getNoteType());
				map.put("userId",userId);
				map.put("result","1");
				map.put("message","找到");
			}else{
				map.put("result","0");
				map.put("message","未找到该笔记");
			}
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}

	//查找笔记
	@RequestMapping(value = "getNote")
	@ResponseBody
	public Map<String,String> getNote(String noteId) {
		Map<String,String> map=new HashMap<String, String>();
			if(noteId!=null){
				Note note=noteService.getNote(noteId);
				if(note!=null){
				Date createTime = note.getNoteTimeCreate();
				Date updateTime = note.getNoteTimeUpdate();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dateCreate = formatter.format(createTime);
				String updateCreate = formatter.format(updateTime);
				map.put("createTime",dateCreate);
				map.put("updateTime",updateCreate);
				map.put("title",note.getNoteTitle());
				map.put("text",note.getNoteText());
				map.put("noteId",noteId);
				map.put("type",note.getNoteType());
				map.put("userId",note.getUser());
				map.put("result","1");
				map.put("message","找到");
			}else{
				map.put("result","0");
				map.put("message","未找到该笔记");
			}
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}

	//修改笔记内容
	@RequestMapping(value = "update_note")
	@ResponseBody
	public Map<String,String> update_note(String userId,String noteId,String title,String text){
		Map<String,String> map=new HashMap<String, String>();
		if(userId!=null&&noteId!=null&&title!=null&&text!=null){
			if(noteService.getUserId(noteId).equals(userId)){
				Date  time= new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String timeUpdate=formatter.format(time);
				noteService.updateNote(userId,noteId,title,text,timeUpdate);
				/*
				修改笔记内容
				修改笔记更新时间Date now = new Date();
				 */
				map.put("result","1");
				map.put("message","修改成功");
			}
			else{
				map.put("result","0");
				map.put("message","笔记id与用户id不对应");
			}
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}

	//修改笔记类型
	@RequestMapping(value = "update_type")
	@ResponseBody
	public Map<String,String> update_type(String userId, String noteId, String type){
		Map<String,String> map=new HashMap<String, String>();
		if(userId!=null&&type!=null&&noteId!=null){
			if(noteService.getUserId(noteId).equals(userId)){
				noteService.updateNoteType(type,userId,noteId);
				map.put("result","1");
				map.put("message","修改成功");
			}
			else{
				map.put("result","0");
				map.put("message","笔记id与用户id不对应");
			}
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}
	//按编号删除笔记
	@RequestMapping(value = "deleteNote")
	@ResponseBody
	public Map<String, String> deleteNote(String userId,String noteId){
		logger.debug("come to deleteNote");
		Map<String,String> map=new HashMap<String, String>();
		if(userId!=null&&noteId!=null){
			if(noteService.getUserId(noteId).equals(userId)){
				noteService.deleteNote("note_id",noteId,userId);
				map.put("result","1");
				map.put("message","删除成功");
			}
			else{
				map.put("result","0");
				map.put("message","笔记id与用户id不对应");
			}
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}
	//清空回收站 note_type 02代表回收站
	@RequestMapping(value = "recycleClear")
	@ResponseBody
	public Map<String,String> recycleClear(String userId,HttpServletRequest request){
		HttpSession session=request.getSession();
		Map<String,String> map=new HashMap<String, String>();
		if(userId!=null){
			if(session.getAttribute("userId").equals(userId)){
				noteService.deleteNote("note_type","02",userId);
				map.put("result","1");
				map.put("message","删除成功");
			}
			else{
				map.put("result","0");
				map.put("message","用户id与会话用户id不对应");
			}
		}else{
			map.put("result","2");
			map.put("message","参数错误");
		}
		return map;
	}
	//获取相关类型的笔记列表

	@RequestMapping(value = "noteList")
	@ResponseBody
	public List<Note> noteList(String userId, String type, String style){
		List<Note> list =new ArrayList<Note>();
		if(userId!=null&&type!=null&&style!=null){
			list=noteService.noteList(userId,type,style);
		}
		return list;
	}
}